package com.wjc.system.controller.admin;


import com.wjc.server.dto.CommonDto;
import com.wjc.server.dto.PageDto;
import com.wjc.server.dto.RoleDto;
import com.wjc.server.service.RoleService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


@RestController
@RequestMapping("/admin/role")
public class RoleController {

    private static final Logger LOG= LoggerFactory.getLogger(RoleController.class);
    public static final String BUSINESS_NAME = "角色";

    @Resource
    private RoleService roleService;

    //查询
    @GetMapping("/list")
    public CommonDto list(@Valid  PageDto pageDto){
         roleService.list(pageDto);
//         LOG.info("分页有啥: {}",pageDto);
        CommonDto<PageDto> pageDtoCommonDto = new CommonDto<>();
        pageDtoCommonDto.setContent(pageDto);
        return pageDtoCommonDto;
    }

    //保存
    @PostMapping("/save")
    public CommonDto list(@Valid @RequestBody RoleDto roleDto){
        CommonDto commonDto=new CommonDto<>();
        roleService.save(roleDto);
        return commonDto;

    }

    //删除
    @DeleteMapping("/delete/{id}")
    public CommonDto delete(@PathVariable Long id){
        CommonDto commonDto=new CommonDto<>();
        roleService.delete(id);
        return commonDto;
    }
    /**
     * 保存资源
     * @param roleDto
     */
    @PostMapping("/save-resource")
    public CommonDto saveResource(@RequestBody RoleDto roleDto) {
        LOG.info("保存角色资源关联开始");
        CommonDto<RoleDto> responseDto = new CommonDto<>();
        roleService.saveResource(roleDto);
        responseDto.setContent(roleDto);
        return responseDto;
    }

    /**
     * 加载已关联的资源
     */
    @GetMapping("/list-resource/{roleId}")
    public CommonDto listResource(@PathVariable Long roleId) {
        LOG.info("加载资源开始");
        CommonDto responseDto = new CommonDto<>();
        List<String> resourceIdList = roleService.listResource(roleId);
        responseDto.setContent(resourceIdList);
        return responseDto;
    }

    /**
     * 保存用户
     * @param roleDto
     */
    @PostMapping("/save-user")
    public CommonDto saveUser(@RequestBody RoleDto roleDto) {
        LOG.info("保存角色用户关联开始");
        CommonDto<RoleDto> responseDto = new CommonDto<>();
        roleService.saveUser(roleDto);
        responseDto.setContent(roleDto);
        return responseDto;
    }
    /**
     * 加载用户
     * @param roleId
     */
    @GetMapping("/list-user/{roleId}")
    public CommonDto listUser(@PathVariable Long roleId) {
        LOG.info("加载用户开始");
        CommonDto responseDto = new CommonDto<>();
        List<String> userIdList = roleService.listUser(roleId);
        responseDto.setContent(userIdList);
        return responseDto;
    }


}
